//
//  STTimerDaySelView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/13.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STTimerDaySelView.h"
#import "STWeekDaySelTimeCell.h"
#import "UIButton+ZYHblock.h"

@implementation STTimerDaySelView


-(void)setTimerDaySelView:(NSArray*)dayArr{
    arrTimerDays = [NSMutableArray arrayWithArray:dayArr];
    [self loadTimerManagerListView];
}

-(void)changeDayTimer:(NSDictionary *)timerDic day:(int)day{
    STHairTimerModel * dayModel ;
    for (int i=0; i<arrTimerDays.count; i++) {
        STHairTimerModel * model = arrTimerDays[i];
        if(model.workday == day){
            dayModel = model;
           
            break;
        }
    }
    if (!dayModel) {
        dayModel = [STHairTimerModel new];
        dayModel.workday =day;
        dayModel.stylistId = [[YLUserInfo sharedInstance].stylistId longValue];
        [arrTimerDays addObject:dayModel];
    }
    
    dayModel.starttime = [NSString stringWithFormat:@"%@:%@:00",timerDic[@"fromHour"],timerDic[@"fromMinute"]];
    dayModel.endtime = [NSString stringWithFormat:@"%@:%@:00",timerDic[@"endHour"],timerDic[@"endMinute"]];
    dayModel.status = 1;
    NSIndexPath * indexpath = [NSIndexPath indexPathForRow:day-1 inSection:0];
    [dayTableView reloadRowAtIndexPath:indexpath withRowAnimation:UITableViewRowAnimationNone];
}


-(void)loadTimerManagerListView{
    if(!dayTableView){
        arrDays = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
        
        dayTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.frame)) style:UITableViewStylePlain];
        dayTableView.delegate = self;
        dayTableView.dataSource = self;
        dayTableView.scrollEnabled = NO;
        dayTableView.backgroundColor = [UIColor whiteColor];
        dayTableView.tableFooterView = [UIView new];
        [dayTableView registerClass:[STWeekDaySelTimeCell class] forCellReuseIdentifier:@"STWeekDaySelTimeCell"];
        [self addSubview:dayTableView];
    }else{
        [dayTableView reloadData];
    }
        
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arrDays.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
//    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.text = @"自定义时间";
    return  titleLabel;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    self.sureBtn = [UIButton new];
    self.sureBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    self.sureBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.sureBtn.backgroundColor = [UIColor clearColor];
    [self.sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal] ;
    [self.sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.sureBtn addTarget:self action:@selector(actionSureDayTimer) forControlEvents:UIControlEventTouchUpInside];
    return  self.sureBtn;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = @"STWeekDaySelTimeCell";
    STWeekDaySelTimeCell * cell =[tableView dequeueReusableCellWithIdentifier:identifier];

    
    [cell setBtnName:arrDays[indexPath.row]];
    STHairTimerModel * dayModel;
    for (int i=0; i<arrTimerDays.count; i++) {
         STHairTimerModel * model = arrTimerDays[i];
        if(model.workday == (indexPath.row+1)){
            dayModel = model;
            break;
        }
    }
    if(!dayModel){
        dayModel = [STHairTimerModel new];
    }
    [cell setWeekSelTime:[self getTimer:dayModel]];
    cell.nameBtn.selected =dayModel.status;
    [cell setBtnBackColor:cell.nameBtn];
    cell.nameBtn.didSelectBtn = ^(UIButton *sender) {
        dayModel.status = !dayModel.status;
    };
    cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    if(indexPath.row == 6){
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSString *)getTimer:(STHairTimerModel *)daytimer{
    NSString * str =[NSString stringWithFormat:@"%@-%@",[daytimer.starttime substringToIndex:5],[daytimer.endtime substringToIndex:5]];
    if (!daytimer.starttime.length ||!daytimer.endtime.length  ) {
        str = @"10:00-23:00";
    }
    return str;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.selectedDayBlock){
         STWeekDaySelTimeCell * cell =[tableView cellForRowAtIndexPath:indexPath];
        self.selectedDayBlock((int)(indexPath.row+1),cell.detailLab.text);
    }
}

-(void)actionSureDayTimer{
    if(self.sureDayBlock){
        self.sureDayBlock(arrTimerDays);
    }
}

@end
